home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / hpgl2ps.zip / USERDEFC.C < prev    next >
C/C++ Source or Header  |  1989-08-08  |  3KB  |  121 lines

  1. /* userdefchar.c */
  2. /*
  3.  * userdefchar(type)
  4.  *
  5.  * function for handling the USER DEFINED CHARACTER (UC) command
  6.  * in Hewlett-Packard Graphics Language (HP-GL).
  7.  *
  8.  * Written by Gerald William Kokodyniak B.A.Sc. M.A.Sc.
  9.  * University of Toronto, Department of Mechanical Engineering
  10.  *
  11.  * Heavily modified by Gordon Jacobs, University of Calif, Berkeley
  12.  *  Now scales user character size to that of regular characters
  13.  *  Orients user character the same as regular characters
  14.  *  Linewidth chosed to always be 0.25mm to better match
  15.  *  postscript font.
  16.  *
  17.  */
  18.  
  19. #include "defn.h"
  20. #define MAXDRAWPOINTS    100
  21.  
  22. /* standard resolution user-def character pen control numbers */
  23. /*     for enhanced, change to -9999,9999 */
  24. #define PENUPCTL -99.0
  25. #define PENDNCTL  99.0
  26.  
  27. /* linewidth in mm for user defined characters */
  28. #define DEFCHAR_LINEWIDTH 0.25
  29.  
  30. /* Scaling parameters */
  31. #define CHAR_CELL_H  5.0
  32. #define CHAR_CELL_W  2.5
  33.  
  34. userdefchar()
  35. {
  36.     float   number, rad, angl;
  37.     float   theMagnitude(), theAngle();
  38.     PENDOWN = 0;
  39.     printf("%g mm setlinewidth\n", DEFCHAR_LINEWIDTH);
  40.  
  41.     while (SIGNED_NUMERIC)
  42.     {
  43.     number = getval();
  44.     if (number >= PENDNCTL) {
  45.         PENDOWN = 1;
  46.         number = getval();
  47.     }
  48.         else if (number <= PENUPCTL) {
  49.         PENDOWN = 0;
  50.         number = getval();
  51.     }
  52.  
  53.         if (number < PENDNCTL && number > PENUPCTL) {
  54.         if (PENDOWN) {
  55.             if (dcount++ >= MAXDRAWPOINTS) {
  56.             end_draw();
  57.             printf("newpath\n");
  58.             printf("  %g %g %s\n", absX, absY, MOVE);
  59.             DRAW_FLAG = 1;
  60.             }
  61.             xval = number / CHAR_CELL_W * char_width * XSCALE;
  62.             yval = getval() / CHAR_CELL_H * char_height * YSCALE;
  63.             if (!DRAW_FLAG) {
  64.             printf("newpath\n");
  65.             printf("  %g %g %s\n", absX, absY, MOVE);
  66.             DRAW_FLAG = 1;
  67.             }
  68.         /* perform rotation */
  69.         rad = theMagnitude(xval,yval);
  70.         angl = theAngle(xval,yval);
  71.         angl += char_angle * deg_rad;
  72.         xval =  rad *cos(angl);
  73.         yval =  rad *sin(angl);
  74.         absX += xval;
  75.             absY += yval;
  76.             printf("  %g %g %s\n", xval, yval, RDRAW);
  77.             } else {
  78.         end_draw();
  79.         xval = number  / CHAR_CELL_W * char_width *XSCALE;
  80.         yval = getval() / CHAR_CELL_H * char_height * YSCALE;
  81.  
  82.         rad = theMagnitude(xval,yval);
  83.         angl = theAngle(xval,yval);
  84.         angl += char_angle * deg_rad;
  85.  
  86.             lastXmove = absX += rad * cos(angl);
  87.             lastYmove = absY += rad * sin(angl);
  88.         }
  89.     }
  90.     else {
  91.         if(number >= PENDNCTL)
  92.         PENDOWN = 1;
  93.         else if(number <= PENUPCTL)
  94.         PENDOWN = 0;
  95.     }
  96.     }
  97.     PENDOWN = 0;
  98.     end_draw();
  99.     /* move a fraction of a space so next character doesn't abutt */
  100.     absX += 0.2 * char_space * cos(char_angle * deg_rad);
  101.     absY += 0.2 * char_space * sin(char_angle * deg_rad);
  102.     printf("%g mm setlinewidth\n", pen_size[pen_number]);
  103. }
  104.  
  105. float
  106. theMagnitude(x,y)
  107. float x,y;
  108. {
  109.     return( sqrt(x*x + y*y) );
  110. }
  111.  
  112. float
  113. theAngle(x,y)
  114. float x,y;
  115. {
  116.     if(x != 0.0)
  117.     return(atan(y/x));
  118.     else
  119.     return((y > 0.0) ? M_PI_2 : (M_PI + M_PI_2));
  120. }
  121.